<template>
  <TablePanel title="线索管理">
    <div slot="btm">
      <div class="topSelect">
              <el-select v-model="operatetypeValue">
                <el-option
                  v-for="dict in operatetype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
      </div>
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                @click="handleAdd"
                v-hasPermi="['crm:leads:add']"
              >新增</el-button>
              <el-button
                type="primary"
                plain
                icon="el-icon-edit"
              >转为客户</el-button>
              <el-button
                type="primary"
                plain
                icon="el-icon-delete"
              >放入线索池</el-button>
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                @click="handleExport"
                v-hasPermi="['crm:leads:export']"
              >导出</el-button>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>







    </div>
    <div slot="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="联系人：" prop="contacts">
          <el-input
            v-model="queryParams.contacts"
            placeholder="请输入联系人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="电话：" prop="telephone">
          <el-input
            v-model="queryParams.telephone"
            placeholder="请输入电话"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div slot="content">
    <el-table v-loading="loading" border :height="tableHeight" :data="leadsList" @selection-change="handleSelectionChange">
      <div slot="empty">
        <st-empty></st-empty>
      </div>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="customer" >
        <template slot-scope="scope">
          <el-button type="text" @click="showLeads(scope.row)">{{scope.row.customer}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="客户性质" align="center" prop="natureDisp" />
      <el-table-column label="客户来源" align="center" prop="sourceDisp" />
      <el-table-column label="客户行业" align="center" prop="industryDisp" />
      <el-table-column label="客户分级" align="center" prop="gradeDisp" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:leads:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:leads:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改线索对话框 -->
    <st-dialog :title="title" v-if="open"
               height="600px"
               @close="cancel"
               @save="submitForm"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
       <el-row>
         <el-col :span="11">
           <el-form-item label="客户名称：" prop="customer">
             <el-input v-model="form.customer" placeholder="请输入客户名称" />
           </el-form-item>
         </el-col>
         <el-col :span="11">
           <el-form-item label="客户性质：" prop="nature">
             <el-select v-model="form.nature" placeholder="请输入客户性质">
               <el-option
                 v-for="dict in dict.type.customer_nature"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
               ></el-option>
             </el-select>
           </el-form-item>
         </el-col>
       </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="客户来源：" prop="source">
              <el-select v-model="form.source" placeholder="请输入客户来源">
                <el-option
                  v-for="dict in dict.type.customer_source"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户行业：" prop="industry">
              <el-select v-model="form.industry" placeholder="请输入客户行业">
                <el-option
                  v-for="dict in dict.type.customer_industry"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="客户分级：" prop="grade">
              <el-select v-model="form.grade" placeholder="请输入客户分级">
                <el-option
                  v-for="dict in dict.type.customer_grade"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户官网：" prop="website">
              <el-input v-model="form.website" placeholder="请输入客户官网" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="联系人：" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="邮箱：" prop="mailbox">
              <el-input v-model="form.mailbox" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="下次联系时间：" prop="nextTime">
              <el-date-picker
                v-model="form.nextTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请输入下次联系时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="电话：" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="所属区域：">
              <st-distpicker
                :province="areaData[0]"
                :city="areaData[1]"
                :area="areaData[2]"
                @province="selectProvince"
                @city="selectCity"
                @area="selectArea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="详细地址：" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="备注：" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="客户简介：" prop="introduction">
              <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </st-dialog>
    <leads-detail v-if="leadsDetailFlag" :params="leadsForm" ref="leadsDetailRef" @close="leadsDetailFlag=false"></leads-detail>
    </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </TablePanel>
</template>

<script>
import { listLeads, getLeads, delLeads, addLeads, updateLeads } from "@/api/crm/leads";
import noData from '@/assets/img/no_data.png'
import StDistpicker from "@/components/StDistpicker/index.vue";
import leadsDetail from './detail/index.vue';
import customerDetail from "@/views/crm/customer/detail/index.vue";
import leads from "@/views/crm/leads/index.vue";
export default {
  name: "Leads",
  computed: {
    leads() {
      return leads
    }
  },
  components: {customerDetail, StDistpicker,leadsDetail},
  dicts: [
    'customer_nature',
    'customer_source',
    'customer_industry',
    'customer_grade',
    'customer_stage',
    'customer_type'
  ],
  data() {
    return {
      leadsDetailFlag:false,
      leadsForm:{},
      noData:noData,
      tableHeight:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线索表格数据
      leadsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contacts: null,
        telephone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      areaData:[],
      operatetypeValue:'all',
      operatetype:[
        {label:'全部',value:'all'},
        {label:'我负责的',value:'mine'},
        {label:'下属负责的',value:'subordinate'},
        {label:'今日待跟进',value:'todayfollowed '},
        {label:'今日已联系',value:'todaycontacted'},
        {label:'待跟进的',value:'followed'},
        {label:'从未跟进的',value:'unfollowed'}
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询线索列表 */
    getList() {
      this.loading = true;
      listLeads(this.queryParams).then(response => {
        this.leadsList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(()=>{
          this.tableHeight=this.getTableHeightWithTitle();
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    showLeads(row){
      this.leadsDetailFlag=true;
      this.leadsForm=row;
      this.$nextTick(()=>{
        this.$refs.leadsDetailRef.show();
      })
    },
    // 表单重置
    reset() {
      this.form = {
        leadsId: null,
        customer: null,
        nature: null,
        source: null,
        industry: null,
        grade: null,
        website: null,
        introduction: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        contacts: null,
        sex: null,
        mailbox: null,
        isTransform: null,
        followup: null,
        nextTime: null,
        telephone: null,
        address: null,
        remark: null,
        createdBy: null,
        ownerUserId: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    selectProvince(value) {
      this.$set(this.form,'provinceId',value.code)
    },
    selectCity(value) {
      this.$set(this.form,'cityId',value.code)
    },
    selectArea(value) {
      this.$set(this.form,'areaId',value.code)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leadsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线索";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const leadsId = row.leadsId || this.ids
      getLeads(leadsId).then(response => {
        this.form = response.data;
        if(typeof this.form.provinceId != "undefined" && this.form.provinceId != null) {
          this.$set(this.areaData,0,+this.form.provinceId)
        }
        if(typeof this.form.cityId != "undefined" && this.form.cityId != null) {
          this.$set(this.areaData,1,+this.form.cityId)
        }
        if(typeof this.form.areaId != "undefined" && this.form.areaId != null) {
          this.$set(this.areaData,2,+this.form.areaId)
        }
        this.open = true;
        this.title = "修改线索";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.leadsId != null) {
            updateLeads(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeads(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leadsIds = row.leadsId || this.ids;
      this.$modal.confirm('是否确认删除线索编号为"' + leadsIds + '"的数据项？').then(function() {
        return delLeads(leadsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/leads/export', {
        ...this.queryParams
      }, `leads_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.el-form{
  text-align: center;
}
.el-select {
  width : 100%;
}
.el-cascader{
  width: 100%;
}
.el-date-editor{
  width: 100%;
}
::v-deep .el-tabs--border-card > .el-tabs__content{
  padding: 0px !important;
}
.topSelect{
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  margin-right: 10px;
}
</style>
